/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    gongwenkai
 * 创建时间:    2023-09-26
 */
package org.jeecg.modules.flow.callback.outbound.task;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptMaterialService;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.third.entity.EmsOrDmsDeliveryDto;
import org.jeecg.modules.business.third.service.IEmsOrDmsDeliveryOperateService;
import org.jeecg.modules.flow.callback.enums.FlowTypeEnum;
import org.jeecg.modules.flow.callback.outbound.util.CommonOutboundUtil;
import org.jeecg.modules.flow.record.entity.WmsFlowRecord;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.flow.sys.entity.dto.WorkFlowInfoDto;
import org.jeecg.modules.flow.sys.service.WorkFlowService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 横剪叫料-600出库准备 回调Bean
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-26
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class OutBoundTaskHJJLTL600OutPrepareBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;

    private final IEmsOrDmsDeliveryOperateService emsOrDmsDeliveryOperateService;

    /**
     * 出库准备
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {

        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String taskId = baseFlowDto.getInfoId();
        baseFlowDto.translateClass(JSONObject.class);
        JSONObject jsonData = (JSONObject) baseFlowDto.getData();
        Boolean fromReceiptOrderFlag = jsonData.getBoolean("fromReceiptOrderFlag");
        if(!Objects.isNull(fromReceiptOrderFlag)&&fromReceiptOrderFlag){
            //说明是前端保存提交出库单转过来的
            //设置任务未为手动模式
            wmsOutReceiptTaskService.update(new LambdaUpdateWrapper<WmsOutReceiptTask>()
                    .set(WmsOutReceiptTask::getFlowType, FlowTypeEnum.MANUAL.getCode())
                    .eq(WmsOutReceiptTask::getId,taskId)
            );
        }else{
            //说明是前端点击取料架
            //设置任务为自动模式
            wmsOutReceiptTaskService.update(new LambdaUpdateWrapper<WmsOutReceiptTask>()
                    .set(WmsOutReceiptTask::getFlowType, FlowTypeEnum.AUTOMATIC.getCode())
                    .eq(WmsOutReceiptTask::getId,taskId)
            );
        }
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(taskId);
    }
}
